package com.yun.zf.system.security.provider;

import com.yun.zf.common.admin.domain.LoginUser;
import com.yun.zf.common.app.domain.SysUserAuth;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.system.redis.RedisCache;
import com.yun.zf.system.security.token.MobileCodeAuthenticationToken;
import com.yun.zf.system.service.ISysMenuService;
import com.yun.zf.system.service.ISysUserService;
import com.yun.zf.system.service.SysUserAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

@Slf4j
@Component
public class MobileCodeAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private SysUserAuthService userAuthService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //认证代码，认证通过返回认证对象，失败返回null
        MobileCodeAuthenticationToken token = (MobileCodeAuthenticationToken) authentication;
        String mobile =token.getMobile();
        String code = token.getCode();
        if (StringUtils.isBlank(mobile)) {
            throw new BadCredentialsException("手机号码不能为空");
        }
        if (StringUtils.isBlank(code)) {
            throw new BadCredentialsException("验证码不能为空");
        }
        // 验证验证码是否正确
        String phoneCode = redisCache.getCacheObject(mobile)==null?null:redisCache.getCacheObject(mobile).toString();
        if (StringUtils.isBlank(phoneCode)){
            throw new BadCredentialsException("验证码已过期");
        }
        if (!code.equals(phoneCode)){
            throw new BadCredentialsException("验证码不正确");
        }
        // 验证账号密码是否正确
        SysUserAuth userAuth = userAuthService.getUserByIphone(Long.parseLong(token.getMobile()), "code");
        LoginUser user = new LoginUser();
        if (userAuth == null) {
            // 说明第一次登录，执行注册逻辑
            throw new BadCredentialsException("账户不存在");
        } else {
            // 获取权限
            Set<String> perms = new HashSet<String>();
            perms.addAll(menuService.selectMenuPermsByUserId(userAuth.getUserId()));
            user.setUserId(userAuth.getUserId());
            user.setPermissions(perms);
        }


        return new MobileCodeAuthenticationToken(user,null);

    }

    @Override
    public boolean supports(Class<?> aClass) {
        return (MobileCodeAuthenticationToken.class.isAssignableFrom(aClass));
    }


    public static void main(String[] args) {

        System.out.println(Math.abs("35423".hashCode())%5);

        System.out.println(Math.abs("35423".hashCode())%8);

        System.out.println(Math.abs("sdlgslj^%^&**gl_3434gsdgs".hashCode())%5);

    }

}
